package com.yztc.orangeentertainment.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.staritembean.FeedListBean;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/9.
 * 动态adapter
 */
public class StarFeedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;

    public void setList(List<FeedListBean> list) {
        this.list = list;
    }

    private List<FeedListBean> list;
    private int ST10 = 10;
    private int ST11 = 11;
    private int ST13 = 13;
    private int ST21 = 21;
    private int ST23 = 23;
    private boolean flag;
    private CharSequence str;
    public StarFeedAdapter(Context context) {
        this.context = context;
    }
    private Handler handler = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message msg) {
            if (msg.what == 0x12) {
                str=(CharSequence) msg.obj;
            }

            return false;
        }
    });

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ST10) {
            //source=1 type=0 对应的视图
            return new MyViewHolder10(LayoutInflater.from(context).inflate(R.layout.item_feed_sourse1_style0, null));
        } else if (viewType == ST11) {
            //source=1 type=1 对应的视图
            return new MyViewHolder11(LayoutInflater.from(context).inflate(R.layout.item_feed_sourse1_style1, null));
        } else if (viewType == ST13) {
            //source=1 type=3 对应的视图
            return new MyViewHolder13(LayoutInflater.from(context).inflate(R.layout.item_feed_sourse1_style3, null));
        } else if (viewType == ST21) {
            //source=2 type=1 对应的视图
            return new MyViewHolder21(LayoutInflater.from(context).inflate(R.layout.item_feed_sourse2_style1, null));
        } else if (viewType == ST23) {
            //source=2 type=3 对应的视图
            return new MyViewHolder23(LayoutInflater.from(context).inflate(R.layout.item_feed_sourse2_style3, null));
        } else {
            return new MyViewHolder11(LayoutInflater.from(context).inflate(R.layout.item_feed_sourse1_style1, null));
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        Log.i("qwe",list.get(position).toString());
        htmlWebPic(list.get(position).getContent());
        if (holder instanceof MyViewHolder10) {
            //source=1 type=0 对应的内容

            //时间转换
            ((MyViewHolder10) holder).tv10Source.setText(context.getResources().getString(R.string.weibo));
            ((MyViewHolder10) holder).tv10Time.setVisibility(View.GONE);
            ((MyViewHolder10) holder).tv10Con.setText(str);
            if (list.get(position).getItem() != null) {
                ((MyViewHolder10) holder).tv10Item.setText(list.get(position).getItem().getAuthor().getName());
                ((MyViewHolder10) holder).tv10ItemCon.setText(list.get(position).getItem().getContent());
            }else {
                ((MyViewHolder10) holder).relFeed10.removeAllViewsInLayout();
            }
        }
        if (holder instanceof MyViewHolder11) {
            //source=1 type=1 对应的内容
            ((MyViewHolder11) holder).tv11Source.setText(context.getResources().getString(R.string.weibo));
            ;

            ((MyViewHolder11) holder).tv11Con.setText(str);

            Glide.with(context).load(list.get(position).getImages().get(0).getThumb()).into(((MyViewHolder11) holder).iv11Thumb);
        }
        if (holder instanceof MyViewHolder13) {
            //source=1 type=3 对应的内容
            ((MyViewHolder13) holder).tv13Source.setText(context.getResources().getString(R.string.weibo));
            ((MyViewHolder13) holder).tv13Time.setVisibility(View.GONE);
            ((MyViewHolder13) holder).tv13Con.setText(str);
            ((MyViewHolder13) holder).tv13Title.setText(list.get(position).getVideo().getTitle());
        }
        if (holder instanceof MyViewHolder21) {
            //source=2 type=1 对应的内容
            ((MyViewHolder21) holder).tv21Source.setText("Instagram");
            ((MyViewHolder21) holder).tv21Time.setVisibility(View.GONE);
            ((MyViewHolder21) holder).tv21Con.setText(str);

            Glide.with(context).load(list.get(position).getImages().get(0).getThumb()).into(((MyViewHolder21) holder).iv21Thumb);
        }
        if (holder instanceof MyViewHolder23) {
            //source=2 type=3 对应的内容
            ((MyViewHolder23) holder).tv23Source.setText("Instagram");
            ((MyViewHolder23) holder).tv23Time.setVisibility(View.GONE);
            ((MyViewHolder23) holder).tv23Con.setText(str);

            ((MyViewHolder23) holder).vv23Src.setVideoURI(Uri.parse(list.get(position).getVideo().getSrc()));
            ((MyViewHolder23) holder).iv23Play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //开始播放视频
                    if (flag == false) {
                        flag = true;
                        ((MyViewHolder23) holder).vv23Src.start();
                        ((MyViewHolder23) holder).iv23Play.setImageResource(R.mipmap.ic_feed_pause);
                        ((MyViewHolder23) holder).vv23Src.setMediaController(new MediaController(context));
                    } else {
                        flag = false;
                        ((MyViewHolder23) holder).iv23Play.setImageResource(R.mipmap.ic_feed_play);
                        ((MyViewHolder23) holder).vv23Src.pause();
                        ((MyViewHolder23) holder).vv23Src.setMediaController(new MediaController(context));
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        //判断source 和 type的值  返回不同的int值
        if (list.get(position).getSource() == 1) {
            if (list.get(position).getType() == 0) {
                return ST10;
            } else if (list.get(position).getType() == 3) {
                return ST13;
            } else {
                return ST11;
            }

        } else {
            if (list.get(position).getType() == 3) {
                return ST23;
            } else {
                return ST21;
            }
        }
    }

    public class Holder extends RecyclerView.ViewHolder {
        //不同source和type对应的viewholder
        public Holder(View itemView) {
            super(itemView);
        }
    }

    /**
     *  1 0  1代表sourse  0代表type
     *  下面类同
     */
    class MyViewHolder10 extends Holder {
        @InjectView(R.id.tv_10_source)
        TextView tv10Source;
        @InjectView(R.id.tv_10_time)
        TextView tv10Time;
        @InjectView(R.id.tv_10_con)
        TextView tv10Con;
        @InjectView(R.id.tv_10_item)
        TextView tv10Item;
        @InjectView(R.id.tv_10_item_con)
        TextView tv10ItemCon;
        @InjectView(R.id.rel_feed10)
        RelativeLayout relFeed10;

        public MyViewHolder10(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    class MyViewHolder11 extends Holder {
        @InjectView(R.id.tv_11_source)
        TextView tv11Source;
        @InjectView(R.id.tv_11_time)
        TextView tv11Time;
        @InjectView(R.id.tv_11_con)
        TextView tv11Con;
        @InjectView(R.id.iv_11_thumb)
        ImageView iv11Thumb;

        public MyViewHolder11(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    class MyViewHolder13 extends Holder {
        @InjectView(R.id.tv_13_source)
        TextView tv13Source;
        @InjectView(R.id.tv_13_time)
        TextView tv13Time;
        @InjectView(R.id.tv_13_con)
        TextView tv13Con;
        @InjectView(R.id.tv_13_title)
        TextView tv13Title;

        public MyViewHolder13(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    class MyViewHolder21 extends Holder {
        @InjectView(R.id.tv_21_source)
        TextView tv21Source;
        @InjectView(R.id.tv_21_time)
        TextView tv21Time;
        @InjectView(R.id.tv_21_con)
        TextView tv21Con;
        @InjectView(R.id.iv_21_thumb)
        ImageView iv21Thumb;

        public MyViewHolder21(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    class MyViewHolder23 extends Holder {
        @InjectView(R.id.tv_23_source)
        TextView tv23Source;
        @InjectView(R.id.tv_23_time)
        TextView tv23Time;
        @InjectView(R.id.tv_23_con)
        TextView tv23Con;
        @InjectView(R.id.vv_23_src)
        VideoView vv23Src;
        @InjectView(R.id.iv_23_play)
        ImageView iv23Play;

        public MyViewHolder23(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    //加载网络表情
    public void htmlWebPic(final String htmlContent) {
        Thread t = new Thread(new Runnable() {
            Message msg = handler.obtainMessage();

            @Override
            public void run() {
                Html.ImageGetter imageGetter = new Html.ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {
                        URL url = null;
                        Drawable drawable = null;
                        try {
                            url = new URL(source);
                            drawable = Drawable.createFromStream(url.openStream(), null);
                            drawable.setBounds(0, 0, 50,50);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return drawable;
                    }
                };
                CharSequence result = Html.fromHtml(htmlContent, imageGetter, null);
                msg.what = 0x12;
                msg.obj = result;
                handler.sendMessage(msg);
            }
        });
        t.start();
    }

}
